﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Datos
{
    public partial class TipoCliente {

        Repository<ArqParcial1Entities> repositorio = new Repository<ArqParcial1Entities>();
        ArqParcial1Entities context = new ArqParcial1Entities();

        public List<TipoCliente> Listar()
        {
            var result = from tipoCliente in context.TipoClientes
                         select tipoCliente;
            return result.ToList();
        }

        public TipoCliente Buscar(int iCodigo)
        {
            var result = from tipoCliente in context.TipoClientes
                         where tipoCliente.tcliCodigo == iCodigo
                         select tipoCliente;
            return result.Count() > 0 ? result.First() : new TipoCliente();
        }

        public List<TipoCliente> Consultar(int? iCodigo, string sDescripcion, decimal? dRangoBajo, decimal? dRangoAlto) {
            var query = from tipoCliente in context.TipoClientes
                         select tipoCliente;
            if(iCodigo == null && string.IsNullOrWhiteSpace(sDescripcion) && dRangoAlto== null && dRangoBajo == null){
                return new List<TipoCliente>();
            }else{
                if (iCodigo != null) {
                    query = query.Where(w => w.tcliCodigo == iCodigo);
                }
                
                if (!string.IsNullOrWhiteSpace(sDescripcion))
                {
                    query = query.Where(w => w.tcliDescripcion.Contains(sDescripcion));
                }
                if (dRangoAlto != null)
                {
                    query = query.Where(w => w.tcliDescuento <= dRangoAlto);
                }

                if (dRangoBajo != null)
                {
                    query = query.Where(w => w.tcliDescuento >= dRangoBajo);
                }
                return query.ToList();
            }
        }

        public bool Guardar(TipoCliente tipoCliente)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<TipoCliente>(tipoCliente);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Actualizar(TipoCliente tipoCliente)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Update<TipoCliente>(tipoCliente);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Eliminar(TipoCliente tipoCliente)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Delete<TipoCliente>(tipoCliente);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
    }
    class TipoClienteMetaData
    {
        public int tcliCodigo { get; set; }
        public string tcliDescripcion { get; set; }
        public decimal tcliDescuento { get; set; }
    }
}
